//
//  TestLoadComponent.cpp
//  Nano
//
//  Created by Jeppe Nielsen on 9/4/13.
//  Copyright (c) 2013 Jeppe Nielsen. All rights reserved.
//

#include "Engine.hpp"
#include "GameState.hpp"
#include "GameWorld.hpp"
#include "RenderSystem.hpp"
#include "Touchable.hpp"
#include "Material.hpp"
#include "TextureComponent.hpp"
#include "FirstPersonMoverSystem.hpp"
#include "TouchSystem.hpp"
#include "Light.hpp"
#include "Sprite.hpp"
#include "Sizeable.hpp"
#include "SpriteMeshSystem.hpp"
#include "FileReader.hpp"

using namespace Nano;

class TestLoadComponent : public GameState {
    void Initialized() {
        Input.TouchUp += event_handler(this, &TestLoadComponent::TouchUp);
        renderer = world.CreateSystem<RenderSystem>();
        world.CreateSystem<FirstPersonMoverSystem>()->Input = &Input;
        world.CreateSystem<TouchSystem>()->Input = &Input;
        world.CreateSystem<SpriteMeshSystem>();
        
        std::string cameraFile = FileReader::GetWritablePath() + "/camera.xml";
        
        GameObject* camera = world.CreateObject();
        camera->AddComponent<Camera>()->Viewport = Viewport();
        camera->AddComponent<Transform>()->Position = Vector3(0,0,4);
        camera->AddComponent<FirstPersonMover>();
        
        
        //std::string file = FileReader::GetWritablePath() + "/quad.xml";
        
        
        gadget = world.CreateObject();
        gadget->AddComponent<Transform>()->Position = Vector3(0,0,-0.05f);
        gadget->AddComponent<Mesh>();
        gadget->AddComponent<Material>()->LightingEnabled = false;
        gadget->AddComponent<Sizeable>()->Size = Vector2(1,1);
        gadget->AddComponent<Sprite>();
        gadget->AddComponent<Colorable>()->Color = Colour::Blue();
        gadget->AddComponent<Touchable>();//->Click += event_handler(this, &TestGUI::GadgetClicked);
        //gadget->Save(file);
        //gadget = world.Load(file);
        
        for (int x=0; x<50; x++) {
            for (int y=0; y<50; y++) {
                
                GameObject* clone = gadget->Clone();
                clone->GetComponent<Transform>()->Position = Vector2(x * 1.5f, y * 1.5f);
                clone->GetComponent<Colorable>()->Color = Colour::HslToRgb(x * 10 + y * 10, 1.0, 1.0, 1.0);
            }
        }
        
        //world.Update(0.1);
        //gadget->EnableComponent<Colorable>(false);
        
    }
    
    void Update(float dt) {
        static float rotation = 0;
        rotation+=dt;
        if (gadget) gadget->GetComponent<Transform>()->Rotation = rotation;
        world.Update(dt);
    }
    
    void Render() {
        glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glEnable(GL_CULL_FACE);
        renderer->Render();
    }
    
    void TouchUp(TouchEvent e) {
       // Exit();
    }
    
    GameWorld world;
    RenderSystem* renderer;
    GameObject* gadget;
};

/*
int main() {
    Engine e;
    e.Start<TestLoadComponent>();
}
 */


